package com.health.system.javacommon.jquery;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.WebUtils;
import cn.org.rapid_framework.page.PageRequest;

public class JqueryPageHelper {

static int DEFAULT_PAGE_SIZE = 10;
	
	public static PageRequest<Map> createPageRequestForJquery(HttpServletRequest request) {
		PageRequest<Map> result = new PageRequest(new HashMap());
		return bindPageRequestParameters(result, request);
	}

	public static PageRequest<Map> bindPageRequestParameters(PageRequest<Map> pageRequest, HttpServletRequest request) {
		int start = getIntParameter(request,"page",1);
		int limit = getIntParameter(request, "rows", DEFAULT_PAGE_SIZE);
		String sort = request.getParameter("sidx");// 指定排序的列
		String dir = request.getParameter("sord");// 顺序倒序
		String orderBy = ""; 
		if (dir != null){
			orderBy = sort + " " + dir;
		}
		//System.out.println( request.getParameter("s_misStartTimeString"));// 顺序倒序
		pageRequest.getFilters().putAll(WebUtils.getParametersStartingWith(request, "s_"));
		
		pageRequest.setPageNumber(start);
		pageRequest.setPageSize(limit);
		pageRequest.setSortColumns(orderBy);
		//pageRequest.get
		return pageRequest;
	}
	
	private static int getIntParameter(HttpServletRequest request,String name,int defaultValue) {
		String value = request.getParameter(name);
		if (StringUtils.isNotBlank(value)) {
			return Integer.valueOf(value);
		}
		return defaultValue;
	}
}
